/*
 * Created on 2005/2/28
 *
 */
package com.erp.app.common.documentLink.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.erp.app.common.documentLink.beans.DocumentLinkIdentifier;
import com.erp.app.common.documentLink.beans.GenericDocumentLinkVO;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.dao.SimplePageEditStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;

public abstract class GenericDocumentLinkDAO extends SimplePageEditStyleDao {

//	protected String sqlPurge(){
//        return sqlDelete();
//    }
//
//    protected void setValuesForPurge(PreparedStatement pStmt, Object obj) throws SQLException {
//        setValuesForDelete(pStmt, obj);
//    }

    /**
     * To be implemented by subclass
     *
     * @return DB name of the view table from which document links are generated
     */
    protected abstract String getViewName();

    /**
     * To be implemented by subclass
     *
     * @return document no of the view table from which document links are generated
     */
    protected abstract String getDocumentNoIdentifier();

    /**
     * To be implemented by subclass
     *
     * @return VO class of the view table from which document links are generated
     */
    protected abstract Class getDocumentLinkVOClass();


    //Override this if needed
    //@return columns that this class doesn't provide
    protected ArrayList getExtraColumns() {
        return null;
    }

    //Override this if needed
    //@return columns that this class doesn't provide
    protected void getMoreResultSetValues(SimpleBaseVo gdlv, ResultSet rs) {
    }


//    protected abstract String getDocumentNumber(SimpleBasePk pk);



    public ChildList selectByParent(SimpleBaseVo parent, Connection conn) throws FindRecordException {
        return super.selectByParent(parent, getDocumentLinkVOClass(), conn);
    }

    // Since we are accessing a view, everything related to update is not needed
    protected String sqlInsert() {
        return null;
    }

    protected void setValuesForInsert(PreparedStatement pStmt, Object obj) throws SQLException {
    }

    protected String sqlUpdate() {
        return null;
    }

    protected void setValuesForModify(PreparedStatement pStmt, Object obj) throws SQLException {
    }

    protected String sqlDelete() {
        return null;
    }

    protected void setValuesForDelete(PreparedStatement pStmt, Object obj) throws SQLException {
    }

    protected String sqlFind() {
        return null;
    }

    protected void setValuesForFind(PreparedStatement pStmt, Object obj) throws SQLException {
    }

    protected String sqlList() {
        StringBuffer sb = new StringBuffer();

        sb.append("SELECT rel_doc_type, rel_doc_no, rel_doc_date, cust_vend_name ");
        sb.append(parseExtraColumns());
        sb.append(" FROM ");
        sb.append(getViewName());
        sb.append(" WHERE company_code = ? AND ");
        sb.append(getDocumentNoIdentifier());
        sb.append(" = ?");

        return sb.toString();
    }

    protected void setValuesForList(PreparedStatement pStmt, Object obj) throws SQLException {
        DocumentLinkIdentifier dli = (DocumentLinkIdentifier) obj;
        int i = 1;
        pStmt.setString(i++, dli.getDocumentLinkCompanyCode());
        pStmt.setString(i++, dli.getDocumentLinkDocNo());
    }

    protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {
        GenericDocumentLinkVO gdlv = null;
        try {
            gdlv = (GenericDocumentLinkVO) getDocumentLinkVOClass().newInstance();
            gdlv.setRelDocType(rs.getString(1));
            gdlv.setRelDocNo(rs.getString(2));
            gdlv.setRelDocDate(rs.getDate(3));
            gdlv.setCounterParty(rs.getString(4));
            getMoreResultSetValues(gdlv, rs);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return gdlv;
    }

    private String parseExtraColumns() {
        Collection columns = getExtraColumns();
        StringBuffer query = new StringBuffer();
        Object column = null;
        boolean isFirstElement = true;

        if (columns == null)
            return "";
            else query.append(", ") ;

        Iterator it = columns.iterator();

        while (it.hasNext()) {
            column = it.next();

            if (!(column instanceof String))
                continue;

            if (!isFirstElement)
                query.append(", ");

            query.append((String) column);
            isFirstElement = false;
        }

        return query.toString();
    }

    protected boolean isViewOnly() {
        return true;
    }

}
